
import { defineStore, createPinia } from "pinia";
import { TagsViewState,tagsViewProps } from "@/store/interface";
export const TagsViewStore = defineStore({
	// id: 必须的，在所有 Store 中唯一
	id: "TagsViewStore",
	// state: 返回对象的函数
  state: (): TagsViewState => ({
    visitedViews: [],
    cachedViews: [],
  }),
  getters: {
  },
  actions: {
    addView(tags: tagsViewProps) {
      if (this.visitedViews.some(v => v.path === tags.path)) return;
      this.visitedViews.push(
        Object.assign({}, tags, {
          title: tags.meta.title || 'no-name'
        })
      ) 
    },
    // 关闭当前
    delView(tags: tagsViewProps) {
      this.visitedViews = this.visitedViews.filter(x=>x.path != tags.path)
      return this.visitedViews
    },
    // 关闭其他
    delOtherView(tags: tagsViewProps) {
      this.visitedViews = this.visitedViews.filter(x=>{
        return x.meta.affix || x.path === tags.path
      })
      return this.visitedViews
    },
    // 关闭右侧
    delRightView(tags: tagsViewProps) {
      this.visitedViews = this.visitedViews.slice(0,this.visitedViews.findIndex(x=>x.path === tags.path) + 1)
      return this.visitedViews
    },
    // 关闭全部
    delAllView() {
      this.visitedViews = this.visitedViews.filter(x=>x.meta.affix)
      return this.visitedViews
    },
    refreshView() {
      
    }
  }
});